package com.sduept.nwld.dataserver.controller.equipment.assessment;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.core.entity.AbstractStandardDict;
import com.sduept.core.service.StandardDictService;
import com.sduept.core.service.impl.StandardDictConfigurator;
import org.apache.commons.lang.StringUtils;
import org.primefaces.PrimeFaces;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;
/**
 * 
 * @author ldx
 *
 */
@Named
@ViewScoped
public class StandardDictController extends AbstractController {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5271134988464428966L;
	//public  static final String libpath = "com.sduept.bigdata.equipment.assessment.model.standarddict.";
	private Class<? extends AbstractStandardDict> T = null;	
	@Autowired
	private StandardDictService sman;
	@Autowired
	private StandardDictConfigurator sdc;
	private AbstractStandardDict current ;
	private AbstractStandardDict selected ;
	private List<AbstractStandardDict> selectedRecords = new ArrayList<AbstractStandardDict>();
	private List<AbstractStandardDict> records = new ArrayList<AbstractStandardDict>();	
	private String dictId;
	
	@SuppressWarnings("unchecked")
	@PostConstruct
	public void init(){
		selected=null;
		dictId = getParameter("dictId");		
		if(!StringUtils.isEmpty(dictId)){
			try {
				/*String cname = libpath+className;
				T = (Class<? extends StandardDict>) Class.forName(cname);				
				T = (Class<? extends AbstractStandardDict>) Class.forName(className); */
				T=sdc.getT(dictId);
				records = (List<AbstractStandardDict>) sman.findAll(Class.forName(T.getName()));				 
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			addErrorMessage("请重新登录！");
		}
	}
	
	public void preCreate(){
		try {
			
			current = T.newInstance();
			current.setOrderNo(records.size()+1);
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	public void create(){
		try {
			boolean isCreate = current.getId() == null;
			current = sman.createOrUpdate(current);
			if(isCreate) records.add(current);
			addSuccessMessage("保存成功！");
		} catch (Exception e) {
			addErrorMessage("保存失败！");
			e.printStackTrace();
		}
	}
	
	public void onRowSelect(SelectEvent event){
		selected = (AbstractStandardDict)event.getObject();
	}
	
	public void closeDlg(){
		current = null;
		addSuccessMessage("closeDlg");
	}
	public void preUpdate(){
		if(selectedRecords.size() > 0)
			current = (AbstractStandardDict) selectedRecords.get(0);
		else 
			current = null;
	}
	public void delete(){
		try {
			AbstractStandardDict[] ss = selectedRecords.toArray(new AbstractStandardDict[0]);
			for(AbstractStandardDict s: ss){
				sman.delete(s.getId(), dictId);
				
				records.remove(s);
			}
			addSuccessMessage("删除成功！");
		} catch (Exception e) {
			addErrorMessage("删除失败！");
			e.printStackTrace();
		}
	}
	
	public void selectedFromDialog(){
		PrimeFaces.current().dialog().closeDynamic(selectedRecords);
	}
	
	/**
	 * @return current
	 */
	public AbstractStandardDict getCurrent() {
		return current;
	}
	/**
	 * @param current 要设置的 current
	 */
	public void setCurrent(AbstractStandardDict current) {
		this.current = current;
	}
	/**
	 * @return selectedRecords
	 */
	public List<AbstractStandardDict> getSelectedRecords() {
		return selectedRecords;
	}
	/**
	 * @param selectedRecords 要设置的 selectedRecords
	 */
	public void setSelectedRecords(List<AbstractStandardDict> selectedRecords) {
		this.selectedRecords = selectedRecords;
	}
	/**
	 * @return records
	 */
	public List<AbstractStandardDict> getRecords() {
		return records;
	}
	/**
	 * @param records 要设置的 records
	 */
	public void setRecords(List<AbstractStandardDict> records) {
		this.records = records;
	}
	public AbstractStandardDict getSelected() {
		return selected;
	}
	public void setSelected(AbstractStandardDict selected) {
		this.selected = selected;
	}

}
